<?php

class AdvertisingAction extends AdminAction
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 广告列表
     */
    public function index()
    {
        //登录用户信息
        $user = Session('admin');
        $user_id = $user['user_id'];

        //分页每页的条数
        $perPage = $this->getPerPage();

        $map['user_id'] = $user_id;
        if (I('get.advertising_name')) {
            $advertising_name = I('get.advertising_name');
            $map['advertising_name'] = array('like', '%'.$advertising_name.'%');
        }

        $currPage = intval($_GET['p']);//当前页数
        $currPage < 1 && $currPage = 1;

        $itemCount = M('Advertising')->where($map)->order('advertising_id desc')->count();

        $itemPerPage = $perPage;//每页的条数
        $pageUrl = $this->baseUrl . '/Admin/Advertising/index';//url
        $paramQuery = array('advertising_name'=>$advertising_name);//参数
        $paginator = new Paginator($currPage, $itemCount, $itemPerPage, $pageUrl, $paramQuery);

        $ads = M('Advertising')->where($map)->order('advertising_id desc')->limit(($currPage-1)*$perPage, $perPage)->select();

        $page = $paginator->getHtml(1); //获取样式为1的分页HTML结构

        $this->assign('baseUrl', $this->baseUrl);
        $this->assign('advertising_name', $advertising_name);
        $this->assign('page', $page);
        $this->assign('ads',$ads);
        $this->display();
    }

    /**
     * 试图：添加广告
     */
    public function add()
    {
        $this->assign('baseUrl', $this->baseUrl);
        $this->assign('ad_types',getAdvertisingType());
        //$this->assign('manage_types',getManageType());
        $this->display();
    }


    /**
     * 动作：添加广告
     */
    public function store()
    {
        //登录用户信息
        $admin = Session('admin');
        //广告主ID
        $user_id = $admin['user_id'];
        $data = array();
        //上传图片处理
        if($_FILES['logo']['name']) {
            $res_log = $this->upload($_FILES['logo']);
            if ($res_log['result'] == 1) {
                $data['logo'] = $res_log['destFile'];
            }
        }
        if ($_FILES['introduce_picture']['name']) {
            $res_introduce_picture = $this->upload($_FILES['introduce_picture']);
            if ($res_introduce_picture['result'] == 1) {
                $data['introduce_picture'] = $res_introduce_picture['destFile'];
            }
        }
        //体验攻略每步图片
        if ( is_numeric(I('post.raiders_num')) &&  I('post.raiders_num')> 0) {
            $num = I('post.raiders_num');
            foreach(range(1,$num) as $val) {
                //体验攻略说明
                $raiders_explain = $_POST['raiders_explain'.$val];
                $data['raiders_explain'] .= $raiders_explain.'[re]';

                $res = $this->upload($_FILES['raiders_picture'.$val]);
                if ($res['result'] == 1) {
                    $data['raiders_picture'] .= $res['destFile'].'[rp]';
                }
            }
        }
        //步骤数
        if ( is_numeric(I('post.step_num')) &&  I('post.step_num')> 0) {
            $step_num = I('post.step_num');
            foreach(range(1,$step_num) as $val) {
                $step_claim = $_POST['step_claim'.$val];
                $step_gift = $_POST['step_gift'.$val];
                $data['step_claim'] .=  $step_claim.'[sc]';
                $data['step_gift']  .=   $step_gift.'[sg]';
            }
        }

        $data['advertising_name']       = I('post.advertising_name');//广告名称
        $data['rate']                   = I('post.rate');//预期年化收益率
        $data['money']                  = I('post.money');//起购金额
        $data['platform']               = I('post.platform');//平台名称
        $data['safe_rating']            = I('post.safe_rating');//安全评级
        $data['siteUrl']                = I('post.siteUrl');//网站地址
        $data['registered_capital']     = I('post.registered_capital');//注册资金
        $data['region']                 = I('post.region');//所在地区
        $data['online_time']            = I('post.online_time');//上线时间
        $data['icp']                    = I('post.icp');//ICP备案
        $data['phone']                  = I('post.phone');//联系电话
        $data['service_email']          = I('post.service_email');//服务邮箱
        $data['company_info']           = I('post.company_info');//公司信息
        $data['jump_url']               = I('post.jump_url');//立即体验跳转地址
        $data['prompt']                 = I('post.prompt');//温馨提示
        $data['experience_time']        = I('post.experience_time');//体验时间
        $data['send_method']            = I('post.send_method');//发送方式：1-自行领取 2-自动发奖
        $data['step_num']               = I('post.step_num');//步骤数
        $data['raiders_num']            = I('post.raiders_num');//体验攻略步骤数
        $data['rule']                   = I('post.rule');//试玩规则
        $data['type']                   = I('post.type');//广告类型
        $data['clearing_num']           = I('post.clearing_num');//广告结算步骤数
        $data['unit_price']             = I('post.unit_price');//广告单价
        $data['total_amount']           = I('post.total_amount');//广告总金额
        $data['alarm_rate']             = I('post.alarm_rate');//广告消耗报警比例
        $data['roi']                    = I('post.roi');//ROI注册与投资转化率
        $data['note']                   = I('post.note');//特殊说明
        //$data['manage_type']            = I('post.manage_type');//广告经营类型(1-平台自营 2-代理经营)(平台自营没有user_id，代理经营有user_id
        $data['create_time']            = time();
        $data['update_time']            = time();

        $data['step_claim']             = trim($data['step_claim'],'[sc]');
        $data['step_gift']              = trim($data['step_gift'],'[sg]');
        $data['raiders_explain']        = trim($data['raiders_explain'],'[re]');
        $data['raiders_picture']        = trim($data['raiders_picture'],'[rg]');
        //广告主信息
        $data['user_id'] = $user_id;//广告所属广告主

        if (M('Advertising')->add($data)) {
            alert('success','增加成功',$this->baseUrl.'/Admin/Advertising/index');
        }
        alert('error','添加失败');
    }

    /**
     * 视图：编辑
     */
    public function edit()
    {
        $advertising_id = I('get.id');
        $map['advertising_id'] = $advertising_id;
        $ad = M('Advertising')->where($map)->find();
        if (!empty($ad) && $ad['type'] == 2) {
            header("Location: " . $this->baseUrl . "/Admin/Advertising/editGame?id=" . $advertising_id);
        }

        //步骤数
        if (is_numeric($ad['step_num']) &&  $ad['step_num']> 0) {
            $step_num_arr = explode('[sc]',$ad['step_claim']);
            $step_gift_arr = explode('[sg]',$ad['step_gift']);
            $ad['step_claim'] = $step_num_arr;
            $ad['step_gift'] = $step_gift_arr;
        }
        //体验攻略
        if ( is_numeric($ad['raiders_num']) &&  $ad['raiders_num']> 0) {

            $ad['raiders_explain'] = explode('[re]',$ad['raiders_explain']);
            $ad['raiders_picture'] = explode('[rp]',$ad['raiders_picture']);

        }

        $this->assign('ad_types',getAdvertisingType());
        $this->assign('manage_types',getManageType());
        $this->assign('ad',$ad);
        $this->display();
    }

    /**
     * 动作：编辑
     */
    public function update()
    {
        //登录用户信息
        $admin = Session('admin');
        //广告主ID
        $user_id = $admin['user_id'];

        $advertising_id = I('post.advertising_id');
        $map['advertising_id'] = $advertising_id;
        $ad = M('Advertising')->where($map)->find();
        if (!$ad) {
            alert('error','该条信息不存在');
        }
        $data = array();
        //上传图片处理
        if($_FILES['logo']['name'])
        {
            $res_log = $this->upload($_FILES['logo']);
            if ($res_log['result'] == 1) {
                $data['logo'] = $res_log['destFile'];
            }
        }
        //介绍图片
        if ($_FILES['introduce_picture']['name']) {
            $res_introduce_picture = $this->upload($_FILES['introduce_picture']);
            if ($res_introduce_picture['result'] == 1) {
                $data['introduce_picture'] = $res_introduce_picture['destFile'];
            }
        }
        //体验攻略每步图片
        if ( is_numeric(I('post.raiders_num')) &&  I('post.raiders_num')> 0) {
            $num = I('post.raiders_num');
            $raiders_picture_arr = explode('[rp]',$ad['raiders_picture']);

            foreach(range(1,$num) as $key=>$val) {
                //体验攻略说明
                $raiders_explain = $_POST['raiders_explain'.$val];
                $data['raiders_explain'] .= $raiders_explain.'[re]';

                $res = $this->upload($_FILES['raiders_picture'.$val]);
                if ($res['result'] == 1) {
                    $raiders_picture_arr[$key] = $res['destFile'];
                }
            }
            $data['raiders_picture'] = join('[rp]',$raiders_picture_arr);

        }
        //步骤数
        if ( is_numeric(I('post.step_num')) &&  I('post.step_num')> 0) {
            $step_num = I('post.step_num');
            foreach(range(1,$step_num) as $val) {
                $step_claim = $_POST['step_claim'.$val];
                $step_gift = $_POST['step_gift'.$val];
                $data['step_claim'] .=  $step_claim.'[sc]';
                $data['step_gift']  .=   $step_gift.'[sg]';
            }
        }
        $data['advertising_name']       = I('post.advertising_name');//广告名称
        $data['rate']                   = I('post.rate');//预期年化收益率
        $data['money']                  = I('post.money');//起购金额
        $data['platform']               = I('post.platform');//平台名称
        $data['safe_rating']            = I('post.safe_rating');//安全评级
        $data['siteUrl']                = I('post.siteUrl');//网站地址
        $data['registered_capital']     = I('post.registered_capital');//注册资金
        $data['region']                 = I('post.region');//所在地区
        $data['online_time']            = I('post.online_time');//上线时间
        $data['icp']                    = I('post.icp');//ICP备案
        $data['phone']                  = I('post.phone');//联系电话
        $data['service_email']          = I('post.service_email');//服务邮箱
        $data['company_info']           = I('post.company_info');//公司信息
        $data['jump_url']               = I('post.jump_url');//立即体验跳转地址
        $data['prompt']                 = I('post.prompt');//温馨提示
        $data['experience_time']        = I('post.experience_time');//体验时间
        $data['send_method']            = I('post.send_method');//发送方式：1-自行领取 2-自动发奖
        $data['step_num']               = I('post.step_num');//步骤数
        $data['raiders_num']            = I('post.raiders_num');//体验攻略步骤数
        $data['rule']                   = I('post.rule');//试玩规则
        $data['type']                   = I('post.type');//广告类型
        $data['clearing_num']           = I('post.clearing_num');//广告结算步骤数
        $data['unit_price']             = I('post.unit_price');//广告单价
        $data['total_amount']           = I('post.total_amount');//广告总金额
        $data['alarm_rate']             = I('post.alarm_rate');//广告消耗报警比例
        $data['roi']                    = I('post.roi');//ROI注册与投资转化率
        $data['note']                   = I('post.note');//特殊说明
        //$data['manage_type']            = I('post.manage_type');//广告经营类型(1-平台自营 2-代理经营)(平台自营没有user_id，代理经营有user_id
        $data['update_time']            = time();

        $data['step_claim']             = trim($data['step_claim'],'[sc]');
        $data['step_gift']              = trim($data['step_gift'],'[sg]');
        $data['raiders_explain']        = trim($data['raiders_explain'],'[re]');
        $data['raiders_picture']        = trim($data['raiders_picture'],'[rp]');
        //广告主信息
        $data['user_id'] = $user_id;//广告所属广告主

        if (M('Advertising')->where($map)->save($data)) {
            alert('success','编辑成功',$this->baseUrl.'/Admin/Advertising/index');
        }
        alert('error','编辑失败');
    }

    /**
     * 动作：删除
     */
    public function delete()
    {
        $id = I('get.id');
        $item =array(
            'ret'=>0,
            'msg'=>''
        );
        $map['advertising_id'] = $id;
        if (M('Advertising')->where($map)->find()) {
            M('Advertising')->where($map)->delete();
            echo json_encode($item);exit;
        }
        $item['ret'] = 1;
        $item['msg'] ='要删除的数据不存在';
        echo json_encode($item);exit;
    }

    /*
     * 添加游戏广告
     */
    public function addGame()
    {
        $this->assign('baseUrl', $this->baseUrl);
        $this->display();
    }

    public function operate()
    {
        //登录用户信息
        $admin = Session('admin');
        //广告主ID
        $user_id = $admin['user_id'];

        $data = array();

        //上传图片处理
        foreach ($_FILES as $key=>$value) {
            if($_FILES[$key]['name']) {
                $res_log = $this->upload($_FILES[$key]);
                if ($res_log['result'] == 1) {
                    $data[$key] = $res_log['destFile'];
                }
            }
        }

        $data['advertising_name']          = I('post.advertising_name','');
        $data['user_id']                   = $user_id;
        $data['type']                      = 2;
        $data['jump_url']                  = I('post.jump_url','');
        $data['rule']                      = I('post.rule','');
        $data['clearing_num']              = I('post.clearing_num', 0);
        $data['unit_price']                = I('post.unit_price', 0);
        $data['total_amount']              = I('post.total_amount', 0);
        $data['alarm_rate']                = I('post.alarm_rate','');
        $data['roi']                       = I('post.roi','');
        $data['note']                      = I('post.note','');
        $data['manage_type']               = 2;
        $data['is_writeable']              = 1;
        $data['create_time']               = time();
        $data['update_time']               = 0;

        $data['game_name']                 = I('post.game_name','');
        $data['game_type']                 = I('post.game_type','');
        $data['game_company']              = I('post.game_company','');
        $data['game_reward']               = I('post.game_reward','');
        $data['game_server']               = I('post.game_server','');
        $data['game_experience_picture']   = I('post.game_experience_picture','');
        $data['game_count_reward']         = I('post.game_count_reward', 0);
        $data['game_begin_time']           = strtotime(I('post.game_begin_time', 0));
        $data['game_end_time']             = strtotime(I('post.game_end_time', 0));
        $data['game_experience_info']      = I('post.game_experience_info','');
        $data['game_step']                 = I('post.game_step', 0);
        $data['game_grade']                = I('post.game_grade', '');
        $data['game_gift']                 = I('post.game_gift', 0);
        $data['game_recharge_info']        = I('post.game_recharge_info', '');
        $data['game_recharge_explanation'] = I('post.game_recharge_explanation', '');
        $data['game_recharge_url']         = I('post.game_recharge_url', '');
        $data['game_introduction']         = I('post.game_introduction', '');
        $data['game_payment_method']       = I('post.game_payment_method', '');

        if (M('advertising')->add($data)) {
            alert('success','增加成功',$this->baseUrl.'/Admin/Advertising/index');
        }
        alert('error','添加失败');

    }

    /*
     * 编辑游戏
     */
    public function editGame()
    {
        $advertising_id = I('get.id');
        $map['advertising_id'] = $advertising_id;
        $ad = M('Advertising')->where($map)->find();
        print_r($ad);

        $this->assign('ad', $ad);
        $this->assign('baseUrl', $this->baseUrl);
        $this->display();
    }

    public function updateGame()
    {
        //登录用户信息
        $admin = Session('admin');
        //广告主ID
        $user_id = $admin['user_id'];

        $data = array();

        $data['advertising_id']            = I('post.advertising_id','');
        $data['advertising_name']          = I('post.advertising_name','');
        $data['user_id']                   = $user_id;
        $data['type']                      = 2;
        $data['jump_url']                  = I('post.jump_url','');
        $data['rule']                      = I('post.rule','');
        $data['clearing_num']              = I('post.clearing_num', 0);
        $data['unit_price']                = I('post.unit_price', 0);
        $data['total_amount']              = I('post.total_amount', 0);
        $data['alarm_rate']                = I('post.alarm_rate','');
        $data['roi']                       = I('post.roi','');
        $data['note']                      = I('post.note','');
        $data['manage_type']               = 2;
        $data['is_writeable']              = 1;
        //$data['create_time']               = time();
        $data['update_time']               = time();

        $data['game_name']                 = I('post.game_name','');
        $data['game_type']                 = I('post.game_type','');
        $data['game_company']              = I('post.game_company','');
        $data['game_reward']               = I('post.game_reward','');
        $data['game_server']               = I('post.game_server','');
        $data['game_experience_picture']   = I('post.game_experience_picture','');
        $data['game_count_reward']         = I('post.game_count_reward', 0);
        $data['game_begin_time']           = strtotime(I('post.game_begin_time', 0));
        $data['game_end_time']             = strtotime(I('post.game_end_time', 0));
        $data['game_experience_info']      = I('post.game_experience_info','');
        $data['game_step']                 = I('post.game_step', 0);
        $data['game_grade']                = I('post.game_grade', '');
        $data['game_gift']                 = I('post.game_gift', 0);
        $data['game_recharge_info']        = I('post.game_recharge_info', '');
        $data['game_recharge_explanation'] = I('post.game_recharge_explanation', '');
        $data['game_recharge_url']         = I('post.game_recharge_url', '');
        $data['game_introduction']         = I('post.game_introduction', '');
        $data['game_payment_method']       = I('post.game_payment_method', '');

        if (M('advertising')->save($data)) {
            alert('success','修改成功',$this->baseUrl.'/Admin/Advertising/index');
        }
        alert('error','修改失败');

    }




}


